package com.yiren.manager.ctrl.cost;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yiren.core.repository.utils.QueryResult;
import com.yiren.core.utils.HTTPResult;
import com.yiren.entity.YrMealCard;
import com.yiren.entity.YrMealsCostRecord;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.cost.YrBaseMealsCostRecordForm;
import com.yiren.manager.form.cost.YrMealsCostRecordForm;
import com.yiren.manager.form.cost.YrMealsCostRecordResultForm;
import com.yiren.manager.form.messhall.YrMealCardForm;
import com.yiren.manager.service.iface.cost.YrMealsCostRecordService;
import com.yiren.manager.service.iface.messhall.YrMealCardService;
import com.yiren.manager.utils.ObjectUtils;

@RequestMapping("mealsCostRecord")
@Controller
public class YrMealsCostRecordController extends GenericController<YrMealsCostRecordForm, YrMealsCostRecord>{

	@Autowired
	private YrMealsCostRecordService selfService;
	
	@Autowired
	private YrMealCardService mealCardService;
	
	@RequestMapping("findByMonth")
	@ResponseBody
	private HTTPResult<YrMealsCostRecordResultForm> findByMonth(String month) throws Exception{
		HTTPResult<YrMealsCostRecordResultForm> result = new HTTPResult<YrMealsCostRecordResultForm>();
		
		String sqlWhere = "DATE_FORMAT(pay_date,'%Y-%m') = ?1";
		LinkedHashMap<String, String> sortMap = new LinkedHashMap<>();
		sortMap.put("payDate", "DESC");
		sortMap.put("createTime", "DESC");
		
		QueryResult<YrMealsCostRecord> qr = selfService.find(YrMealsCostRecord.class,sqlWhere, new Object[]{month},sortMap);
		
		Map<String, List<YrMealsCostRecord>> map = new HashedMap();
		
		for(YrMealsCostRecord entity : qr.getResultList()){
			if(map.containsKey(entity.getMealsType())){
				map.get(entity.getMealsType()).add(entity);
			}else {
				List<YrMealsCostRecord> list = new ArrayList<YrMealsCostRecord>();
				list.add(entity);
				map.put(entity.getMealsType(), list);
			}
		}
		
		List<YrMealsCostRecordResultForm> formList = new ArrayList<YrMealsCostRecordResultForm>();
	
		for(Map.Entry<String, List<YrMealsCostRecord>> entry : map.entrySet()){
			YrMealsCostRecordResultForm form = new YrMealsCostRecordResultForm();
			form.setMealType(entry.getKey());

			List<YrBaseMealsCostRecordForm> baseMealsCostRecordForms = new ArrayList<YrBaseMealsCostRecordForm>();
			
			for(YrMealsCostRecord entity : entry.getValue()){
				boolean flag = false;
				for(YrBaseMealsCostRecordForm recordForm : baseMealsCostRecordForms){
					if(entity.getMealsName().equals(recordForm.getMealsName()) && (entity.getPayMoney()-recordForm.getPayMoney()==0)){
						recordForm.setQuantity(recordForm.getQuantity()+1);
						flag = true;
						break;
					}
				}
				
				if(!flag){
					YrBaseMealsCostRecordForm baseMealsCostRecordForm = new YrBaseMealsCostRecordForm();
					baseMealsCostRecordForm.setMealsName(entity.getMealsName());
					baseMealsCostRecordForm.setQuantity(1);
					baseMealsCostRecordForm.setPayMoney(entity.getPayMoney());
					baseMealsCostRecordForms.add(baseMealsCostRecordForm);
				}
			}
			form.setBaseMealsCostRecordForms(baseMealsCostRecordForms);
			formList.add(form);
		}

		result.setData(formList);
		return result;
	}
	
	@RequestMapping("findByDay")
	@ResponseBody
	private HTTPResult<YrMealsCostRecordResultForm> findByDay(String day) throws Exception{
		HTTPResult<YrMealsCostRecordResultForm> result = new HTTPResult<YrMealsCostRecordResultForm>();
		
		String sqlWhere = "DATE_FORMAT(pay_date,'%Y-%m-%d') = ?1";
		LinkedHashMap<String, String> sortMap = new LinkedHashMap<>();
		sortMap.put("createTime", "DESC");
		
		QueryResult<YrMealsCostRecord> qr = selfService.find(YrMealsCostRecord.class,sqlWhere, new Object[]{day},sortMap);
		
		System.out.println(qr.getTotalRecord());
		Map<String, List<YrMealsCostRecord>> map = new HashedMap();
		
		for(YrMealsCostRecord entity : qr.getResultList()){
			if(map.containsKey(entity.getMealsType())){
				map.get(entity.getMealsType()).add(entity);
			}else {
				List<YrMealsCostRecord> list = new ArrayList<YrMealsCostRecord>();
				list.add(entity);
				map.put(entity.getMealsType(), list);
			}
		}
		
		List<YrMealsCostRecordResultForm> formList = new ArrayList<YrMealsCostRecordResultForm>();
	
		for(Map.Entry<String, List<YrMealsCostRecord>> entry : map.entrySet()){
			YrMealsCostRecordResultForm form = new YrMealsCostRecordResultForm();
			form.setMealType(entry.getKey());

			List<YrBaseMealsCostRecordForm> baseMealsCostRecordForms = new ArrayList<YrBaseMealsCostRecordForm>();
			
			for(YrMealsCostRecord entity : entry.getValue()){
				boolean flag = false;
				for(YrBaseMealsCostRecordForm recordForm : baseMealsCostRecordForms){
					if(entity.getMealsName().equals(recordForm.getMealsName()) && (entity.getPayMoney()-recordForm.getPayMoney()==0)){
						recordForm.setQuantity(recordForm.getQuantity()+1);
						flag = true;
						break;
					}
				}
				
				if(!flag){
					YrBaseMealsCostRecordForm baseMealsCostRecordForm = new YrBaseMealsCostRecordForm();
					baseMealsCostRecordForm.setMealsName(entity.getMealsName());
					baseMealsCostRecordForm.setQuantity(1);
					baseMealsCostRecordForm.setPayMoney(entity.getPayMoney());
					baseMealsCostRecordForms.add(baseMealsCostRecordForm);
				}
			}
			form.setBaseMealsCostRecordForms(baseMealsCostRecordForms);
			formList.add(form);
		}

		result.setData(formList);
		return result;
	}
	
	@RequestMapping("findDetailByDay")
	@ResponseBody
	public HTTPResult<YrMealsCostRecordForm> findDetailByDay(String day) throws Exception{
		HTTPResult<YrMealsCostRecordForm> result = new HTTPResult<YrMealsCostRecordForm>();
		String sqlWhere = "DATE_FORMAT(pay_date,'%Y-%m-%d') = ?1";
		LinkedHashMap<String, String> sortMap = new LinkedHashMap<>();
		sortMap.put("createTime", "DESC");
		QueryResult<YrMealsCostRecord> qr = selfService.find(YrMealsCostRecord.class,sqlWhere, new Object[]{day},sortMap);
	
		List<YrMealsCostRecordForm> formList = new ArrayList<YrMealsCostRecordForm>();
		for(YrMealsCostRecord entity : qr.getResultList()){
			YrMealsCostRecordForm form = toForm(entity);
			formList.add(form);
		}
		
		result.setData(formList);
		return result;
	}
	
	
	@RequestMapping("findPersonMealsCostRecord")
	@ResponseBody
	public HTTPResult<YrMealCardForm> findPersonMealsCostRecord(String startDay,String endDay,String searchVal) throws Exception{
		HTTPResult<YrMealCardForm> result = new HTTPResult<YrMealCardForm>();
		String sqlWhere = "card_person_name=?1 OR card_card_id=?2";
		
		QueryResult<YrMealCard> qrMealCard = mealCardService.find(YrMealCard.class,sqlWhere, new Object[]{searchVal,searchVal});
		if(qrMealCard.getTotalRecord()==0){
			throwExp("查询不到饭卡信息");
		}
		
		YrMealCard mealCard  = qrMealCard.getResultList().get(0);
		
		YrMealCardForm mealCardForm = ObjectUtils.objectToObject(mealCard, YrMealCardForm.class);
		QueryResult<YrMealsCostRecord> qrMealsCostRecord = null;
		LinkedHashMap<String, String> sortMap = new LinkedHashMap<>();
		sortMap.put("payDate", "DESC");
		sortMap.put("createTime", "DESC");
		
		if(startDay==null||endDay==null){
			sqlWhere = "user_name=?1 OR user_card_id=?2";
			qrMealsCostRecord = selfService.find(YrMealsCostRecord.class, sqlWhere, new Object[]{searchVal,searchVal},sortMap);
		}else {
			sqlWhere = "DATE_FORMAT(pay_date,'%Y-%m-%d')>=?1 AND DATE_FORMAT(pay_date,'%Y-%m-%d')<=?2 AND (user_name=?3 OR user_card_id=?4)";
			qrMealsCostRecord = selfService.find(YrMealsCostRecord.class, sqlWhere, new Object[]{startDay,endDay,searchVal,searchVal},sortMap);
		}
	
		List<YrMealsCostRecordForm> mealsCostRecordForms = new ArrayList<YrMealsCostRecordForm>();
		for(YrMealsCostRecord mealsCostRecord : qrMealsCostRecord.getResultList()){
			mealsCostRecordForms.add(toForm(mealsCostRecord));
		}
		
		List<YrMealCardForm> formList = new ArrayList<YrMealCardForm>();
		mealCardForm.setMealsCostRecordForms(mealsCostRecordForms);
		formList.add(mealCardForm);
		result.setData(formList);
		
		return result;
	}
	
	@Override
	public YrMealsCostRecordForm toForm(YrMealsCostRecord e) throws Exception {
		YrMealsCostRecordForm form = ObjectUtils.objectToObject(e, YrMealsCostRecordForm.class);
		return form;
	}

	@Override
	public YrMealsCostRecord toEntity(YrMealsCostRecordForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
